#include "Lang.h"
#include "io/FileInputStream.h"
using namespace yzrilyzr_io;
using namespace yzrilyzr_lang;
namespace yzrilyzr_util{
	Lang::Lang(){
		currentLocale=Locale::getDefault()->toString();
	}
	std::vector<String > Lang::availableLanguages(){
		std::vector<String > keys(map.size());
		for(auto & k : map){
			keys.push_back(k.first);
		}
		return keys;
	}
	std::unordered_map<String, std::shared_ptr<Properties>> & Lang::getMap(){
		return map;
	}
	void Lang::setCurrentLocale(const String & current){
		currentLocale=current;
	}
	String Lang::getCurrentLocale()const{
		return currentLocale;
	}
	void Lang::add(std::shared_ptr<Locale> locale, std::shared_ptr<Properties> props){
		map[locale->toString()]=props;
	}
	void Lang::add(std::shared_ptr<Properties> props){
		map[currentLocale]=props;
	}
	String Lang::get(const String & key)const{
		auto it=map.find(currentLocale);
		if(it != map.end()){
			return it->second->get(key);
		}
		return key;
	}
	const wchar_t * Lang::getcw(const String & key)const{
		static thread_local std::wstring loc2;
		loc2=get(key).towstring();
		return loc2.c_str();
	}
	const char16_t * Lang::getc16(const String & key)const{
		static thread_local std::u16string loc2;
		loc2=get(key).tou16string();
		return loc2.c_str();
	}

	const char * Lang::getc(const String & key)const{
		static thread_local std::string loc2;
		loc2=get(key).tostring(UTF8);
		return loc2.c_str();
	}
	String Lang::get(std::shared_ptr<Locale> locale, const String & key)const{
		auto it=map.find(locale->toString());
		if(it != map.end()){
			return it->second->get(key);
		}
		return key;
	}
}